package com.ruoyi.sumfun.blog.service.impl;

import com.ruoyi.common.base.domain.BaseMapper;
import com.ruoyi.common.base.service.impl.BaseServiceImpl;
import com.ruoyi.sumfun.blog.mapper.ArticleMapper;
import com.ruoyi.sumfun.blog.mapper.CategoryMapper;
import com.ruoyi.sumfun.blog.mapper.TagMapper;
import com.ruoyi.sumfun.blog.service.ArticleService;
import com.ruoyi.system.api.domain.blog.Article;
import com.ruoyi.system.api.domain.blog.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 超前探信息Service业务层处理
 *
 * @author linespacee
 * @date 2024-01-16
 */
@Service
public class ArticleServiceImpl extends BaseServiceImpl<Article> implements ArticleService
{
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    protected BaseMapper<Article> getEntityMapper() {
        return articleMapper;
    }

    @Override
    public List<Article> selectList(Article t) {
        List<Article> articles = articleMapper.selectList(t);
        // 假设你已经有了Article对象article，并且它的tag_ids字段是"1,2,3,4,6"
        for (Article article : articles) {
            String tagIds = article.getTagId();
            String[] ids = tagIds.split(","); // 将字符串分割为ID数组
            List<String> tagNames = new ArrayList<>();
            // 遍历ID数组，针对每个ID查询tag表
            for (String id : ids) {
                // 这里假设你有一个方法来根据ID查询Tag对象
                String tagName = tagMapper.findTagById(Integer.parseInt(id.trim())); // 假设tagMapper是你的MyBatis Mapper
                if (tagName != null) {
                    tagNames.add(tagName);
                }
            }
            article.setTagNames(tagNames);
        }
        return articles;
    }

    @Override
    public Map<String, Integer> selectCount() {
        Map<String, Integer> counts = new HashMap<>();
        counts.put("article", articleMapper.selectCount());
        counts.put("tag", tagMapper.selectCount());
        counts.put("category", categoryMapper.selectCount());
        return counts;

    }
}
